package com.example.pageability;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.window.dialog.ToastDialog;
import ohos.event.notification.NotificationRequest;
import ohos.rpc.IRemoteObject;
import java.util.Timer;
import java.util.TimerTask;


public class ServiceAbility extends Ability {

    void showToast(String msg) {
        ToastDialog toastDialog = new ToastDialog(this.getContext());
        toastDialog.setText(msg);
        toastDialog.show();
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        showToast("onStart");
        NotificationRequest request = new NotificationRequest(1005);
        NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
        content.setTitle("服务").setText("音乐播放");
        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
        request.setContent(notificationContent);

// 绑定通知，1005为创建通知时传入的notificationId
        keepBackgroundRunning(1005, request);
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                terminateAbility();
            }
        };
        Timer timer = new Timer();
        timer.schedule(task, 2000);
    }

    @Override
    public void onBackground() {
        super.onBackground();
        showToast("onBackground");
    }

    @Override
    public void onStop() {
        super.onStop();
        showToast("onStop");
        cancelBackgroundRunning();
    }

    @Override
    public void onCommand(Intent intent, boolean restart, int startId) {
        showToast("onCommand" + intent.getStringParam("data"));
    }

    @Override
    public IRemoteObject onConnect(Intent intent) {
        showToast("onConnect");
        return null;
    }

    @Override
    public void onDisconnect(Intent intent) {
        showToast("onDisconnect");
    }
}